package com.sgb.jicai.viewmodel;

import android.text.TextUtils;
import androidx.lifecycle.ViewModel;
import com.sgb.jicai.base.JiCaiBaseApi;
import com.sgb.jicai.entity.JiCaiJoinRequireEntity;
import com.swgk.core.base.api.ICallBack;
import com.sgb.jicai.entity.JiCaiGroupBuyingRuleEntity;
import com.sgb.jicai.entity.JiCaiRequireEntity;
import com.sgb.jicai.view.fragment.JoinRequireFragment;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class JoinRequireFraModel  extends ViewModel {

    private JoinRequireFragment fragment;

    public void getJoinRequire(String id) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getJoinRequire(id).enqueue(new Callback<BaseEntity<JiCaiJoinRequireEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<JiCaiJoinRequireEntity>> call, Response<BaseEntity<JiCaiJoinRequireEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    MToast.showToast(fragment.getContext(), "获取加盟规则失败");
                    return;
                }
                if (response.body().getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    updateOfferRequireView(response.body().getData());
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<JiCaiJoinRequireEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 重新组装 报价规则数据
     * @param entity
     */
    public void updateOfferRequireView(JiCaiJoinRequireEntity entity) {
        //组装列表数据并展示
        List<JiCaiRequireEntity> dataList = new ArrayList<>();
        if (!TextUtils.isEmpty(entity.getDepositRatio()) ) {
            dataList.add(new JiCaiRequireEntity("定金比例:", entity.getDepositRatio() + "%"));
        }
        if (!TextUtils.isEmpty(entity.getPayRatio())&& !"0".equals(entity.getPayRatio()) ) {
            dataList.add(new JiCaiRequireEntity("付款比例:", entity.getPayRatio()));
        }
        if (!TextUtils.isEmpty(entity.getLowBuy())) {
            dataList.add(new JiCaiRequireEntity("采购起始量:", entity.getLowBuy()+entity.getUnitName()));
        }
        if (!TextUtils.isEmpty(entity.getPayType())) {
            dataList.add(new JiCaiRequireEntity("付款方式:", entity.getPayType()));
        }
        if (!TextUtils.isEmpty(entity.getFinalPaymentTime())&&!"0".equals(entity.getFinalPaymentTime())) {
            dataList.add(new JiCaiRequireEntity("尾款支付周期:", entity.getFinalPaymentTime() + "天"));
        }
        if (null != entity.getJoinRequireCustomizeFields() && entity.getJoinRequireCustomizeFields().size() > 0) {
            for (int i = 0; i < entity.getJoinRequireCustomizeFields().size(); i++) {
                JiCaiRequireEntity jiCaiRequireEntity = entity.getJoinRequireCustomizeFields().get(i);
                dataList.add(new JiCaiRequireEntity(jiCaiRequireEntity.getName() + ": ", jiCaiRequireEntity.getValue()));
            }
        }
        fragment.updateView(dataList);
    }

    /**
     * 集采规则
     * @param id
     */
    public void getGroupBuyingRule(String id) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getGroupBuyingRule(id).enqueue(new ICallBack<BaseEntity<JiCaiGroupBuyingRuleEntity>>() {
            @Override
            public void onSuccess(BaseEntity<JiCaiGroupBuyingRuleEntity> data) {
                if (data == null || data.getState() == null) {
                    MToast.showToast(fragment.getContext(), "获取报价规则失败");
                    return;
                }
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    updateGroupBuyingRuleView(data.getData());
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }

            @Override
            public void onFail() {
                super.onFail();
            }
        });
    }

    /**
     * 重新组装 集采规则数据
     * @param entity
     */
    private void updateGroupBuyingRuleView(JiCaiGroupBuyingRuleEntity entity) {
        //组装列表数据并展示
        try {
            List<JiCaiRequireEntity> dataList = new ArrayList<>();
           /* if (!TextUtils.isEmpty(entity.fi())) {
                dataList.add(new JiCaiRequireEntity("退货要求", entity.getReturnRequirement()));
            }*/
            if (!TextUtils.isEmpty(entity.getStandardOfServiceCharges())) {
                dataList.add(new JiCaiRequireEntity("服务费收取", entity.getStandardOfServiceChargesString()));
                switch (entity.getStandardOfServiceCharges()){
                    case "1":
                        if (!TextUtils.isEmpty(entity.getStandardOfServiceRatio())) {
                            dataList.add(new JiCaiRequireEntity("收取比例", entity.getStandardOfServiceRatio()+"%"));
                        }
                        break;
                    case "2":
                        if (!TextUtils.isEmpty(entity.getStandardOfServiceCost())) {
                            dataList.add(new JiCaiRequireEntity("费用", entity.getStandardOfServiceCost()+"元"));
                        }
                        break;
                    case "3":
                        if (!TextUtils.isEmpty(entity.getStandardOfServiceCost())&&!entity.getStandardOfServiceCost().equals("0")&&!entity.getStandardOfServiceCost().equals("0.00")&&!entity.getStandardOfServiceCost().equals("0.0")) {
                            dataList.add(new JiCaiRequireEntity("供方承担", entity.getStandardOfServiceCost()));
                        }
                    case "4":
                        if (!TextUtils.isEmpty(entity.getStandardOfServiceCost())&&!entity.getStandardOfServiceCost().equals("0")&&!entity.getStandardOfServiceCost().equals("0.00")&&!entity.getStandardOfServiceCost().equals("0.0")) {
                            dataList.add(new JiCaiRequireEntity("需方承担", entity.getStandardOfServiceCost()));
                        }
                        break;
                }
            }
            if (!TextUtils.isEmpty(entity.getLoadType())) {
                dataList.add(new JiCaiRequireEntity("装车要求", entity.getLoadType()));
            }
            if (!TextUtils.isEmpty(entity.getUploadType())) {
                dataList.add(new JiCaiRequireEntity("卸车要求", entity.getUploadType()));
            }
            if (!TextUtils.isEmpty(entity.getDeliveryMethodString())) {
                dataList.add(new JiCaiRequireEntity("送货方式", entity.getDeliveryMethodString()));
                switch (entity.getDeliveryMethod()) {
                    case "1": //自提
                        break;
                    case "2": //供方配送
                        if (!TextUtils.isEmpty(entity.getFreeKil())) {
                            dataList.add(new JiCaiRequireEntity("免费配送距离", entity.getFreeKil()+"公里"));
                        }
                        if (!TextUtils.isEmpty(entity.getExtraRatesCost())) {
                            dataList.add(new JiCaiRequireEntity("超出收费标准", "每"+entity.getExtraRates()+"公里，费用"+entity.getExtraRatesCost()+"元/"+entity.getUnitName() ));
                        }
                        break;
                    case "3": // 发起方配送
                        if (entity.getFreightRules().size() > 0) {
                            for (int i = 0; i < entity.getFreightRules().size(); i++) {
                                dataList.add(new JiCaiRequireEntity("公里数("+(i+1)+")", entity.getFreightRules().get(i).getKilometers()+"   费用(元/"+entity.getUnitName()+")   "+ entity.getFreightRules().get(i).getFreight()));
                            }
                        }
                        break;
                }
            }
            if (!TextUtils.isEmpty(entity.getQualityRequirement())) {
                dataList.add(new JiCaiRequireEntity("质量要求", entity.getQualityRequirement()));
            }
            if (!TextUtils.isEmpty(entity.getReturnRequirement())) {
                dataList.add(new JiCaiRequireEntity("退货要求", entity.getReturnRequirement()));
            }
            if (!TextUtils.isEmpty(entity.getInstallationRequirement())) {
                dataList.add(new JiCaiRequireEntity("安装要求", entity.getInstallationRequirement()));
            }
            if (!TextUtils.isEmpty(entity.getWarrantyRequirement())) {
                dataList.add(new JiCaiRequireEntity("质保要求", entity.getWarrantyRequirement()));
            }
            if (!TextUtils.isEmpty(entity.getRepairRequirement())) {
                dataList.add(new JiCaiRequireEntity("维修约定", entity.getRepairRequirement()));
            }
            if (!TextUtils.isEmpty(entity.getPaymentRequirement())) {
                dataList.add(new JiCaiRequireEntity("付款约定", entity.getPaymentRequirement()));
            }
            if (!TextUtils.isEmpty(entity.getLogisticsRequirement())) {
                dataList.add(new JiCaiRequireEntity("物流约定", entity.getLogisticsRequirement()));
            }

            if (null != entity.getGroupbuyingRuleCustomizeFields() && entity.getGroupbuyingRuleCustomizeFields().size() > 0) {
                dataList.addAll(entity.getGroupbuyingRuleCustomizeFields());
            }
            fragment.updateView(dataList);
        } catch (Exception e) {
            fragment.updateView(new ArrayList<>());
        }
    }


    public void setActivity(JoinRequireFragment joinRequireFragment) {
        this.fragment = joinRequireFragment;
    }
}
